package com.wsp.controllermanagement.controller;

import com.wsp.apimanagement.pojo.dto.Result;
import com.wsp.apimanagement.pojo.entity.Comment;
import com.wsp.apimanagement.pojo.entity.Record;
import com.wsp.apimanagement.pojo.vo.RecordInfo;
import com.wsp.apimanagement.service.RecordService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * Package: com.wsp.controllermanagement.controller
 * Description： TODO
 *
 * @Author: 巫思平
 * Copyright: Copyright (c) 2020
 * Version: 1
 */
@Api(tags = "打卡记录(说说)请求接口")
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;


    @ApiOperation("发布打卡（有图片）")
    @PostMapping("/putRecord")
    public Result<Record> putRecord(
            @RequestParam("moduleId") Long moduleId,
            @RequestParam("userId") Long userId,
            @RequestParam("recordContent") String recordContent,
            @RequestParam("redis") MultipartFile file) throws IOException {
        //文件名
        String fileName = file.getOriginalFilename();
        FileInputStream fileInputStream = (FileInputStream) file.getInputStream();
        BufferedInputStream bufferedInputStream =new BufferedInputStream(fileInputStream);
        //获取文件后缀名
        assert fileName != null;
        String suffixName = fileName.substring(fileName.lastIndexOf("."));

        return recordService.putRecord(moduleId,userId,recordContent,bufferedInputStream,suffixName);
    }

    @ApiOperation("发布打卡（没有图片）")
    @PostMapping("/putRecordWithoutImg")
    public Result<Record> putRecordWithoutImg(
            @RequestParam("moduleId") Integer moduleId,
            @RequestParam("userId") Integer userId,
            @RequestParam("recordContent") String recordContent) {
        return recordService.putRecordWithoutImg(moduleId,userId,recordContent);
    }

    @ApiOperation("点赞打卡")
    @GetMapping("/likeRecord")
    public Result<Integer> likeRecord(
            Integer userId,
            Integer recordId){
        return recordService.likeRecord(String.valueOf(userId),String.valueOf(recordId));
    }

    @ApiOperation("通过用户id查找打卡记录")
    @GetMapping("/getRecordListByUserId/{userId}/{moduleId}/{currentPage}")
    public Result<List<Record>> getRecordListByUserId(
            @PathVariable("userId") Long userId,
            @PathVariable("moduleId") Long moduleId,
            @PathVariable("currentPage") Integer currentPage){
        return recordService.findRecordListByUserId(userId, moduleId, currentPage);
    }

    @ApiOperation(("发表评论"))
    @PostMapping("/insertComment")
    public Result<?> insertComment(@Validated Comment comment){
        return recordService.putComment(comment);
    }

    @ApiOperation(("获取打卡记录详细信息"))
    @GetMapping("/getRecordInfo/{recordId}/{userId}")
    public Result<RecordInfo> getRecordInfo(@PathVariable("recordId") @NotNull Long recordId, @PathVariable("userId") Long userId){
        return recordService.getRecordInfo(recordId,userId);
    }

}
